(function(window) {
    function Progress($progressBar, $progressLine, $progressDot) {
        return new Progress.prototype.init($progressBar, $progressLine, $progressDot);
    }
    Progress.prototype = {
        constructor: Progress,
        init: function($progressBar, $progressLine, $progressDot) {
            this.$progressBar = $progressBar;
            this.$progressLine = $progressLine;
            this.$progressDot = $progressDot;
        },
        isMove: false,
        startVoice: 1,
        startProgress: 0,
        progressClick: function(callBack) {
            var $this = this; // 此时此刻的this是progress
            // 监听背景的点击
            this.$progressBar.click(function(event) {
                // 获取背景距离窗口默认的位置
                var normalLeft = $(this).offset().left;
                // 获取点击的位置距离窗口的位置
                var eventLeft = event.pageX;
                //定义点击位置
                var move = eventLeft - normalLeft;
                // 设置前景的宽度
                $this.$progressLine.css("width", move);
                $this.$progressDot.css("left", move - 7);
                // 计算进度条的比例
                var value = (eventLeft - normalLeft) / $(this).width();
                $this.startVoice = value;
                callBack(value);
                // console.log("11");
            });
        },
        progressMove: function(callBack, voiceBack) {
            var $this = this;
            // 获取背景距离窗口默认的位置
            var normalLeft = this.$progressBar.offset().left;
            var barWidth = this.$progressBar.width();
            var eventLeft;
            // 1.监听鼠标的按下事件
            this.$progressBar.mousedown(function() {
                // console.log("33");
                $this.isMove = true;
                // 2.监听鼠标的移动事件
                $(document).mousemove(function(event) {
                    // 获取点击的位置距离窗口的位置
                    eventLeft = event.pageX;
                    var offset = eventLeft - normalLeft;
                    if (offset >= 0 && offset <= barWidth) {
                        // 设置前景的宽度
                        $this.$progressLine.css("width", offset);
                        $this.$progressDot.css("left", offset - 7);
                        // 计算进度条的比例
                        var value = offset / barWidth;
                        $this.startVoice = value;
                        // console.log($this.startVoice, value);
                        voiceBack($this.startVoice);
                    }
                    // console.log($this.startVoice, value);
                });
                // 3.监听鼠标的抬起事件
                $(document).one("mouseup", (function(event) {
                    // console.log("22");
                    $(document).off("mousemove");
                    //自动计时
                    $this.isMove = false;
                    // 计算进度条的比例
                    eventLeft = event.pageX;
                    var offset = eventLeft - normalLeft;
                    if (offset >= 0 && offset <= barWidth) {
                        var value = offset / barWidth;
                        $this.startProgress = value;
                    } else if (offset > barWidth) {
                        $this.startProgress = 1;
                    } else {
                        $this.startProgress = 0;
                    }
                    // console.log($this.startProgress);
                    callBack($this.startProgress);
                }));
            });

        },
        setProgress: function(value) {
            if (this.isMove) return;
            if (value < -1.04 || value > 100) return;
            this.$progressLine.css({
                width: value + "%"
            });
            this.$progressDot.css({
                left: value + "%"
            });
        },

    }
    Progress.prototype.init.prototype = Progress.prototype;
    window.Progress = Progress;
})(window);